package com.emily.test.controller;

import com.alibaba.fastjson.JSONObject;
import com.emily.test.controller.result.ResponseResult;
import com.emily.test.model.ONavigation;
import com.emily.test.service.NavigationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author ：zc
 * @date ：Created in 2019/3/26 16:20
 * @description：导航接口
 */
@RestController
@RequestMapping(value = "navigation")
@Api(value = "官网导航", description = "导航接口", position = 1, produces = MediaType.APPLICATION_JSON_VALUE)
public class NavigationController {
    @Autowired
    private NavigationService navigationService;
    private Logger logger = LoggerFactory.getLogger(CompanyNavigationController.class);

    @ApiOperation(value = "获取导航列表", notes = "获取导航信息", httpMethod = "POST")
    @PostMapping(value = "/getNList")
    public ResponseResult getList() {
        try {
            return ResponseResult.success("0", "查询成功", navigationService.getNavigationList());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }

    @ApiOperation(value = "获取导航信息", notes = "获取导航信息", httpMethod = "POST")
    @PostMapping(value = "/getNavigation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "导航id", required = true)
    })
    public ResponseResult getInfo(@RequestBody(required = false) JSONObject id) {
        try {
            logger.info("获取信息{}", id.toJSONString());
            return ResponseResult.success("0", "查询成功", navigationService.getNavigation(id.getInteger("id")));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }

    @ApiOperation(value = "删除导航", notes = "根据导航id删除导航信息", httpMethod = "POST")
    @PostMapping(value = "/delNavigation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "导航id", required = false)
    })
    public ResponseResult delNavigation(@RequestBody JSONObject id) {
        try {
            logger.info("获取信息{}", id.toJSONString());
            navigationService.delete(id.getInteger("id"));
            return ResponseResult.success("0", "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("查询失败");
        }
    }

    @ApiOperation(value = "新增导航", notes = "增加导航信息", httpMethod = "POST")
    @PostMapping(value = "/addNavigation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "navigationName", value = "导航名称", required = false),
            @ApiImplicitParam(name = "tabName", value = "数据库表明", required = false)
    })
    public ResponseResult addNavigation(@RequestBody JSONObject jsonObject) {
        try {
            logger.info("获取信息{}", jsonObject.toJSONString());
            String navigationName = jsonObject.getString("navigationName");
            String tabName = jsonObject.getString("tabName");
            ONavigation oNavigation = new ONavigation();
            oNavigation.setNavigationName(navigationName);
            oNavigation.setTabName(tabName);
            navigationService.insert(oNavigation);
            return ResponseResult.success("0", "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("失败");
        }
    }

    @ApiOperation(value = "导航修改", notes = "修改导航信息", httpMethod = "POST")
    @PostMapping(value = "/modNavigation")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "导航id", required = true),
            @ApiImplicitParam(name = "navigationName", value = "导航名称", required = false),
            @ApiImplicitParam(name = "tabName", value = "数据库表明", required = false)
    })
    public ResponseResult modNavigation(@RequestBody JSONObject jsonObject) {
        try {
            logger.info("获取信息{}", jsonObject.toJSONString());
            Integer id = jsonObject.getInteger("id");
            String navigationName = jsonObject.getString("navigationName");
            String tabName = jsonObject.getString("tabName");
            ONavigation oNavigation = navigationService.getNavigation(id);
            oNavigation.setNavigationName(navigationName);
            oNavigation.setTabName(tabName);
            navigationService.update(oNavigation);
            return ResponseResult.success("0", "新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.fail("失败");
        }
    }

}
